/*************************************************************************
	> File Name: test3.cc
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/22 18:58
 ************************************************************************/

#include <iostream>
#include <pthread.h>
#include <unistd.h>

int tickets = 1000;

void *getTickets(void *args)
{
    (void)args;
    while (1) {
        if (tickets > 0) {
            usleep(100);
            printf("%p:%d\n", (void*)pthread_self(), tickets);
            --tickets;
        } else {
            break;
        }
    }
    return nullptr;
}

int main(int argc, char *argv[])
{
    pthread_t t1, t2,t3;
    pthread_create(&t1, nullptr, getTickets, nullptr);
    pthread_create(&t2, nullptr, getTickets, nullptr);
    pthread_create(&t3, nullptr, getTickets, nullptr);
    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    return 0;
}
